(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.writeOff.updateController", updateController);
	updateController.$inject = [ "$scope", "modalService", "gridService", "layoutConfig", "validatorConfig", "authService", "repaymentService" ];
	function updateController($scope, modalService, gridService, layoutConfig, validatorConfig, authService, repaymentService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.repayment = {};

		ctrl.init = function() {
			authService.updateInit(params).then(function(response) {
				repaymentService.getWriteOffLayout(response.data.model.loan.loanInfo.prodSubtype, response.data.model.loan.loanInfo.prodType, "update").then(function(layout) {
					ctrl.layoutConfig = layout;
					ctrl.repayment = response.data.model;
					ctrl.repayment.taskId = response.data.task.taskId;
					ctrl.repayment.repayType = "W";
					ctrl.repayment.payType_Show = false;
					repaymentService.initData(ctrl.repayment);
					displayWfInfo(response);
				});
			});
		};
		ctrl.finalRepayFlagChanged = function(repayment) {
			repaymentService.finalRepayFlagChanged(repayment);
		};

		ctrl.repayAmtChanged = function(repayment) {
			repaymentService.repayAmtChanged(repayment);
		};

		ctrl.payPrincAmtChanged = function(repayment) {
			repaymentService.payPrincAmtChanged(repayment);
		};

		ctrl.repayTypeChanged = function(repayment) {
			repaymentService.repayTypeChanged(repayment, ctrl.layoutConfig.formConfigs.repayment.gridConfigs);
		};
		function displayWfInfo(response) {
			var messages = [];

			if (response.data.task.taskProps.wfInfo) {
				var msg = new Object();
				msg.args = response.data.task.taskProps.wfInfo.msgParams || "";
				msg.msg = response.data.task.taskProps.wfInfo.msgCode || "cls.error.unknow.exception";
				messages.push(msg);
			}
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			}
		}
		;
		ctrl.save = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				ctrl.repayment.repayPrincAmt = ctrl.repayment.repayAmt;
				return authService.updateSaved(ctrl.repayment).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.settlement = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				ctrl.repayment.repayPrincAmt = ctrl.repayment.repayAmt;
				return authService.updateSubmitted(ctrl.repayment).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.cancel = function() {
			modalService.openPromptDialogs({
				"btnCallback" : {
					"ok" : function(comments) {
						ctrl.repayment.comments = comments;
						authService.updateCancelled(ctrl.repayment).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}

		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.cancel" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}
	}
})(angular);
